package com.example.test_activity.MVP.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.test_activity.MVP.presenter.AddPresenter;
import com.example.test_activity.MVP.presenter.IAddPresenter;
import com.example.test_activity.R;
import com.example.test_activity.entity.SerializableObject;
import com.example.test_activity.entity.Week;

import java.util.Map;

public class AddCourseActivity extends Activity implements IAdd{

    Button button;
    EditText editText_1;        //课程名
    Spinner spinner_min;        //最小周次
    Spinner spinner_max;        //最大周次
    Spinner spinner_week;        //单双周
    EditText editText_7;        //教学楼
    EditText editText_8;        //教室

    private String num;
    private String weekend;
    SerializableObject map = new SerializableObject();

    private IAddPresenter addPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        num = getIntent().getStringExtra("num");
        weekend = getIntent().getStringExtra("weekend");

        init();
        addPresenter = new AddPresenter(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Week week = new Week();
                week.setName(editText_1.getText().toString());
                week.setMin(spinner_min.getSelectedItem().toString());
                week.setMax(spinner_max.getSelectedItem().toString());
                week.setFlag(spinner_week.getSelectedItem().toString());
                week.setOne(editText_7.getText().toString());
                week.setTwo(editText_8.getText().toString());

                addPresenter.doDecide(week);

            }
        });
    }

    private void init() {
        editText_1 = (EditText) findViewById(R.id.e_1);
        editText_7 = (EditText) findViewById(R.id.e_7);
        editText_8 = (EditText) findViewById(R.id.e_8);

        spinner_min = (Spinner) findViewById(R.id.spinner_min);
        spinner_max = (Spinner) findViewById(R.id.spinner_max);
        spinner_week = (Spinner) findViewById(R.id.spinner_week);

        button = (Button) findViewById(R.id.btn_send);
        spinner_min();
        spinner_week();
    }

    private void spinner_min()
    {
        String[] min = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,min);
        spinner_min.setAdapter(arrayAdapter);
        spinner_max.setAdapter(arrayAdapter);
        spinner_min.setSelection(0);
        spinner_max.setSelection(15);
    }

    private void spinner_week()
    {
        String[] item = {"每周","单周","双周"};
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,item);
        spinner_week.setAdapter(arrayAdapter);
        spinner_week.setSelection(0);
    }


    private void sendMessage(Week week)
    {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();

        addPresenter.setCourse(week,weekend,num);
        bundle.putSerializable("map", map);
        intent.putExtras(bundle);

        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void decide(int num,Week week) {
        switch (num)
        {
            case 1:
                Toast.makeText(AddCourseActivity.this, "课程名不能为空", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(AddCourseActivity.this, "教学楼不能为空", Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(AddCourseActivity.this, "教室不能为空", Toast.LENGTH_SHORT).show();
                break;
            default:
                sendMessage(week);
                break;
        }
    }

    @Override
    public void getCourse(Map<String, String> course) {
        map.setMap(course);
    }
}
